import logging

from nicegui import ui

from opencv_calibration_frame import OpencvCalibrationFrame
from opencv_defect_frame import OpencvDefectFrame
from opencv_positioning_frame import OpencvPositioningFrame

_LOGGER = logging.getLogger(__name__)

handler_screen = logging.StreamHandler()
handler2 = logging.FileHandler(filename="./log/opencvmain.log")

_LOGGER.setLevel(logging.DEBUG)
handler_screen.setLevel(logging.DEBUG)
handler2.setLevel(logging.DEBUG)

formatter = logging.Formatter("%(asctime)s %(name)s %(levelname)s %(message)s")
handler_screen.setFormatter(formatter)
handler2.setFormatter(formatter)

_LOGGER.addHandler(handler_screen)
_LOGGER.addHandler(handler2)

TAB_PROPS = 'vertical dense inline-label active-bg-color=primary active-color=white indicator-color=primary'


class OpencvMainUi:
    def __init__(self, logger: logging.Logger):
        self.logger = logger
        self._build()

    def _build(self):
        with ui.header().classes(replace='row items-center') as header:
            ui.button(on_click=lambda: ui.notify('Copied to clipboard', type='positive', color='primary'),
                      icon='menu').props('flat color=white')
            with ui.tabs().props('active-bg-color=indigo-1 active-color=blue-14 indicator-color=indigo-1') as tabs:
                ui.tab(name='tab_opencv', label='', icon='photo_library')
                ui.tab(name='tab_settings', label='', icon='settings')
                ui.tab(name='tab_about', label='', icon='info')

        with ui.tab_panels(tabs, value='tab_opencv').props('vertical').classes('w-full h-full min-h-screen'):
            with ui.tab_panel('tab_opencv'):
                with ui.splitter(value=12).classes('w-full h-full min-h-screen') as vkpsplitter:
                    with vkpsplitter.before:
                        with ui.tabs().props(TAB_PROPS).classes('w-full') as tabs_opencv:
                            tab_opencv_calibration = ui.tab('tab_opencv_calibration', label='算法标定',
                                                         icon='center_focus_strong').classes(
                                'justify-start mr-4')
                            tab_opencv_positioning = ui.tab('tab_opencv_positioning', label='定位检测',
                                                         icon='center_focus_strong').classes(
                                'justify-start mr-4')
                            tab_opencv_defect = ui.tab('tab_defect', label='缺陷检测',
                                                       icon='lens_blur').classes('justify-start mr-4')
                    with vkpsplitter.after:
                        with ui.tab_panels(tabs_opencv, value=tab_opencv_calibration).classes('w-full'):
                            with ui.tab_panel(tab_opencv_calibration):
                                OpencvCalibrationFrame(self.logger)
                            with ui.tab_panel(tab_opencv_positioning):
                                OpencvPositioningFrame(self.logger)
                            with ui.tab_panel(tab_opencv_defect):
                                OpencvDefectFrame(self.logger)
            with ui.tab_panel('tab_settings'):
                with ui.row().classes('w-full'):
                    ui.label('Saved Profiles').classes('text-h6')
                with ui.row().classes('w-full'):
                    ui.label('v0.1 All rights reserved by BECKIOT')
            with ui.tab_panel('tab_about'):
                with ui.row().classes('w-full'):
                    ui.label('Supported algorithms').classes('text-h6')
                with ui.row().classes('w-full'):
                    ui.label('v0.1 All rights reserved by BECKIOT')


@ui.page('/')
def main():
    OpencvMainUi(_LOGGER)


ui.run()
